#!/usr/bin/env python

import argparse
import json
import os
import pipes
import sys

def main():
    parser = argparse.ArgumentParser(description='Fake Swift Compiler.')
    parser.add_argument('--add-dep', type=str, nargs='+')
    parser.add_argument('-module-name', type=str)
    parser.add_argument('-output-file-map', type=str)
    parser.add_argument('-incremental', action='store_true')
    parser.add_argument('-emit-library', dest='emit_library', action='store_true')
    parser.add_argument('-emit-module', action='store_true')
    parser.add_argument('-emit-module-path', type=str)
    parser.add_argument('-emit-dependencies', action='store_true')
    parser.add_argument('-parse-as-library', action='store_true')
    parser.add_argument('-whole-module-optimization', action='store_true')
    parser.add_argument('-num-threads', type=int) 
    parser.add_argument('-I', type=str)
    parser.add_argument('-O', type=str)
    parser.add_argument('-o', dest='output_file', type=str)
    parser.add_argument('-c', dest="compile", action='store_true')
    parser.add_argument('-###', dest="show_commands", action='store_true')
    parser.add_argument('-v', dest="verbose", action='store_true')
    parser.add_argument('--version', dest="version", action='store_true')
    parser.add_argument('sources', type=str, nargs='*')
    args = parser.parse_args()

    # Parse the output file map.
    if args.output_file_map:
        with open(args.output_file_map) as f:
            output_file_map = json.load(f)
    else:
        output_file_map = None

    # If run in verbose mode, print a fake version.
    if args.verbose or args.version:
        print "Pseudo Swift version 1.2.3 (%s)" % (
            os.environ.get("PSEUDO_SWIFT_VERSION", "12.1"),)
        print("Target: bla bla bla")
        if args.version:
            return
    
    # If run in show commands mode, print some dummy output.
    if args.show_commands:
        print ' '.join(map(pipes.quote, [
            sys.argv[0], "-frontend", "...blablabla..."]))
        return
        
    # Write dummy outputs.
    if output_file_map is not None:
        writeAllDeps = True
        deps = None
        if args.whole_module_optimization: 
            # Write only one dep file for entire module.
            writeAllDeps = False
            deps = output_file_map[""]["dependencies"]
            if os.path.exists(deps):
                os.remove(deps)
            # Non-parallel WMO mode.
            if not args.num_threads > 0: 
                # Write only one object file for entiremodule.
                object = output_file_map[""]["object"]
                writeObject(object) 
                writeDeps(object, deps, args.add_dep, False)
                return

        for key,item in output_file_map.items():
            if key == "":
                continue
            object = item["object"]
            writeObject(object) 
            # Write dummy dependencies.
            if writeAllDeps:
                deps = item["dependencies"]
            writeDeps(object, deps, args.add_dep, not writeAllDeps)
    if args.emit_library:
        writeObject(args.output_file)

def writeDeps(object, deps, add_dep, append):
    with open(deps, "a" if append else "w") as f:
        f.write("%s: %s\n" % (object, ' '.join(add_dep or [])))

def writeObject(object):
    with open(object, "w") as f:
        f.write("<dummy>")

if __name__ == '__main__':
    main()
